<template>
  <div>
    <!-- 表格 -->
    <el-table ref="multipleTable" :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
      :style="{height: tableHeight}" @select="selectChange" @select-all="selectAll" style="overflow: auto;">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" prop="apiCode" label="接口资源编码"></el-table-column>
      <el-table-column align="center" prop="apiName" label="接口资源名称"></el-table-column>
      <el-table-column align="center" prop="apiUri" label="接口资源URI"></el-table-column>
      <el-table-column align="center" prop="apiMethod" label="接口资源请求方法"></el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {
    getAuthApiList
  } from '@/api/sys/menu/index'
  import {
    authorizeApi
  } from '@/api/sys/role/index'

  /* 工具 */
  import {
    mapGetters
  } from 'vuex'
  import * as common from '@/utils/base/common'

  export default {
    /* 按钮资源列表 */
    name: 'api-list',
    data() {
      return {
        tableHeight: (parseInt(parseInt(document.documentElement.clientHeight)) / 2.5) + 'px',

        restPath: 'sys/user',
        list: [],
        tableKey: 0,
        listLoading: false,

        menuId: '',
      }
    },
    props: ['roleId'],
    methods: {

      initData(menuId) {
        this.menuId = menuId
        this.list = []
        this.getList()
      },

      // 后台获取列表数据
      getList() {
        this.listLoading = true;
        getAuthApiList(this.roleId, this.menuId).then(response => {
          this.list = response.data
          this.listLoading = false;
          this.list.forEach(item => {
            if (item.authorized) {
              setTimeout(() => {
                this.$refs.multipleTable.toggleRowSelection(item, true);
              }, 100)
            }
          });
        }).catch(err => {
          this.listLoading = false;
          common.message(this, 'error', 2, '调用接口失败!')
          console.error(err)
        })
      },

      selectChange(selection, row) {
        this.authorize(this.getSelectIds(selection))
      },

      selectAll(selection) {
        this.authorize(this.getSelectIds(selection))
      },

      /**
       * 授权
       * @param {Object} ids  所选按钮资源id集合
       */
      authorize(ids) {
        let myData = {
          roleId: this.roleId,
          menuId: this.menuId,
          apiIds: ids
        }
        authorizeApi(myData).then(response => {
            if (response.code === 200) {
              common.message(this, 'success', 2, '授权成功!')
              this.getList()
            } else {
              common.tips(this, response.message)
            }
          }).catch(err => {
            common.message(this, 'error', 2, '授权失败!')
            console.error(err)
          })
      },

      resetData() {
        this.list = []
      },

      getSelectIds(selection) {
        let userIds = []
        for (let i = 0; i < selection.length; i++) {
          userIds.push(selection[i].id)
        }
        return userIds;
      },

    }
  }
</script>

<style scoped>
  @import "~@/styles/list.css";
</style>
